﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Numerics;

namespace BaseMod
{
    public struct Point3D
    {
        private double x;
        public double X
        {
            get => Math.Round(x,1);
            set => x = value;
        }
        private double y = 0.0;
        public double Y
        {
            get => Math.Round(y, 1);
            set => y = value;
        }
        private double z = 0.0;

        public double Z
        {
            get => Math.Round(z, 1);
            set => z = value;
        }
        public Point3D(double x, double y, double z)
        {
            X = x;
            Y = y;
            Z = z;
        }
    }

    public class FourPoint2
    {
        public static bool AreCoplanar(Point3D a, Point3D b, Point3D c, Point3D d)
        {
            // 构建三个向量 
            Vector3 ab = new Vector3(b.X - a.X, b.Y - a.Y, b.Z - a.Z);
            Vector3 ac = new Vector3(c.X - a.X, c.Y - a.Y, c.Z - a.Z);
            Vector3 ad = new Vector3(d.X - a.X, d.Y - a.Y, d.Z - a.Z);

            // 计算混合积：ab · (ac × ad)
            Vector3 cross = CrossProduct(ac, ad);
            double dot = DotProduct(ab, cross);

            Vector3 cross1 = CrossProduct(ab, ac);
            double dot1 = DotProduct(ad, cross1);

            Vector3 cross2 = CrossProduct(ab, ad);
            double dot2 = DotProduct(ac, cross2);

            return Math.Abs(dot) < 1e-10;
        }

        private static Vector3 CrossProduct(Vector3 v1, Vector3 v2)
        {
            return new Vector3(
                v1.Y * v2.Z - v1.Z * v2.Y,
                v1.Z * v2.X - v1.X * v2.Z,
                v1.X * v2.Y - v1.Y * v2.X
            );
        }

        private static double DotProduct(Vector3 v1, Vector3 v2)
        {
            return v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z;
        }

    }
}
